/// @ProjectName : hc_ui_demo
/// @Author :
/// @Time : 2023-04-19 17:03
/// @Description : //todo
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/utils/hc_gradient_util.dart';

enum HcDividerStyle { solid, dashed }

class HcDivider extends StatelessWidget {
  //方向
  final Axis decoration;

  //颜色
  final Color? color;

  //厚度
  final double thickness;

  //距离开始距离
  final double indent;

  //距离结束距离
  final double endIndent;

  //间距
  final double spacing;

  //分隔符样式
  final HcDividerStyle style;

  //分隔符数量
  final int? dashedCounts;

  const HcDivider(
      {super.key,
      required this.decoration,
      this.color,
      this.thickness = 0.0,
      this.indent = 0.0,
      this.endIndent = 0.0,
      this.spacing = 12,
      this.style = HcDividerStyle.solid,
      this.dashedCounts});

  @override
  Widget build(BuildContext context) {
    //方向
    bool isVertical = decoration == Axis.vertical;
    //颜色
    Color finalColor = color ?? Theme.of(context).dividerColor;
    //实线下的边框
    BorderSide borderSide = BorderSide(color: finalColor, width: thickness);
    //如果是实线则按照设定的间距 如果是虚线则比较线段宽度与间距哪个更大
    double finalSpacing = style == HcDividerStyle.dashed || isVertical
        ? max(spacing, thickness)
        : spacing;
    return SizedBox(
      height: !isVertical ? finalSpacing : null,
      width: isVertical ? finalSpacing : null,
      child: Center(
        child: Container(
          height: !isVertical ? thickness : null,
          width: isVertical ? thickness : null,
          margin: EdgeInsets.only(
            top: isVertical ? indent : 0,
            bottom: isVertical ? endIndent : 0,
            left: !isVertical ? indent : 0,
            right: !isVertical ? endIndent : 0,
          ),
          decoration: BoxDecoration(
              gradient: style == HcDividerStyle.dashed
                  ? HcGradientUtil.generateDividerDashed(
                      color: finalColor,
                      isVertical: isVertical,
                      dashedCounts: dashedCounts)
                  : null,
              border: style == HcDividerStyle.solid
                  ? Border(
                      bottom: !isVertical ? borderSide : BorderSide.none,
                      left: isVertical ? borderSide : BorderSide.none)
                  : null),
        ),
      ),
    );
  }
}
